// CtlButton.cpp : implementation file
//

#include "stdafx.h"
#include "bdc.h"
#include "CtlButton.h"

using namespace bdc::ctl;

// CtlButton

IMPLEMENT_DYNAMIC(CtlButton, CButton)

CtlButton::CtlButton()
{
	pImgBg = NULL;
	isMouseIn = false;
}

CtlButton::~CtlButton()
{
}


BEGIN_MESSAGE_MAP(CtlButton, CButton)
	ON_WM_MOUSEMOVE()
	ON_WM_MOUSELEAVE()
END_MESSAGE_MAP()



// CtlButton message handlers




void CtlButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	// TODO:  Add your code to draw the specified item
	if(!pImgBg) return;

	RECT cRect = lpDrawItemStruct->rcItem;
	Gdiplus::Graphics graphics(lpDrawItemStruct->hDC);

	bool isSelected = lpDrawItemStruct->itemState & ODS_SELECTED;
	if(isSelected) {
		Gdiplus::Rect rect(0, 0, cRect.right, cRect.bottom);
		graphics.DrawImage(pImgBg, rect, 0, pImgBg->GetHeight() / 3 * 2, cRect.right, cRect.bottom, Gdiplus::UnitPixel);
	}
	else if(isMouseIn) {
		Gdiplus::Rect rect(0, 0, cRect.right, cRect.bottom);
		graphics.DrawImage(pImgBg, rect, 0, 0, cRect.right, cRect.bottom, Gdiplus::UnitPixel);
	}
	else {
		Gdiplus::Rect rect(0, 0, cRect.right, cRect.bottom);
		graphics.DrawImage(pImgBg, rect, 0, pImgBg->GetHeight() / 3, cRect.right, cRect.bottom, Gdiplus::UnitPixel);
	}
}


void CtlButton::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: Add your message handler code here and/or call default
	if(!isMouseIn){
		this->Invalidate();
	}

	//MessageBox(_T("mouse over"));
	isMouseIn = true;

	TRACKMOUSEEVENT trackMouse = {0};
	trackMouse.cbSize = sizeof(TRACKMOUSEEVENT);
	trackMouse.hwndTrack = this->m_hWnd;
	trackMouse.dwFlags = TME_LEAVE;

	TrackMouseEvent(&trackMouse);

	CButton::OnMouseMove(nFlags, point);
}


void CtlButton::OnMouseLeave()
{
	// TODO: Add your message handler code here and/or call default
	isMouseIn = false;

	this->Invalidate();

	CButton::OnMouseLeave();
}

void CtlButton::setBgImg(Gdiplus::Image* pImg)
{
	pImgBg = pImg;
}
